package stbm.gui;

import java.awt.*;
import javax.swing.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import java.awt.event.*;
import java.util.Random;

import stbm.guihelpers.*;
import stbm.objects.*;

/**
 * The class representing the Game screen
 * 
 * @author Nirav B.
 * @author Nisha L.
 * @this is a test commit from CJ
 */

public class GamePanel extends BasicPanel {

	JButton tradeButton;

	JButton flyButton;

	JButton fuelUp;

	JRadioButton RojoButton, AzulButton, AmarilloButton, VerdeButton,
			NaranjaButton;

	ButtonGroup allPlanets;

	JLabel locationLabel, fuelLabel;

	private static boolean pickPlanet = false;

	public GamePanel(GameManager gameManager) {

		super(gameManager);
		this.setLayout(new GridBagLayout());
		GridBagConstraints mpConstraints = new GridBagConstraints();

		mpConstraints.gridx = 0;
		mpConstraints.gridy = 0;
		JLabel label = new JLabel(
				"<html><h1>Welcome to SpaceTrader!</h1></html>");
		this.add(label, mpConstraints);

		mpConstraints.gridy = 1;
		locationLabel = new JLabel("<html><h2>Welcome to the planet "
				+ gameManager.universe.curPlanet.name + "</h2></html>"); // +
		// gameManager.universe.curPlanet.name);

		// mpConstraints.gridy = 0;
		this.add(locationLabel, mpConstraints);

		mpConstraints.gridy = 2;
		fuelLabel = new JLabel("Current fuel: " + gameManager.ship.getTank()); // +
		// gameManager.ship.getTank());
		this.add(fuelLabel, mpConstraints);

		// flyButton = new JButton("Move to planet");
		// this.add(flyButton);
		// flyButton.addActionListener(new ClickListener());

		mpConstraints.gridy = 3;

		JPanel actionsPanel = new JPanel();

		FlowLayout apLayout = new FlowLayout();
		apLayout.setHgap(20);

		actionsPanel.setLayout(apLayout);

		actionsPanel
				.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

		JPanel planetSelectPanel = new JPanel(new GridBagLayout());
		GridBagConstraints pspConstraints = new GridBagConstraints();
		pspConstraints.gridx = 0;
		pspConstraints.gridy = 0;

		RojoButton = new JRadioButton("Rojo");
		RojoButton.addActionListener(new PickPlanetListener(
				gameManager.universe.planets.get("Rojo")));
		AzulButton = new JRadioButton("Azul");
		AzulButton.addActionListener(new PickPlanetListener(
				gameManager.universe.planets.get("Azul")));
		AmarilloButton = new JRadioButton("Amarillo");
		AmarilloButton.addActionListener(new PickPlanetListener(
				gameManager.universe.planets.get("Amarillo")));
		VerdeButton = new JRadioButton("Verde");
		VerdeButton.addActionListener(new PickPlanetListener(
				gameManager.universe.planets.get("Verde")));
		NaranjaButton = new JRadioButton("Naranja");
		NaranjaButton.addActionListener(new PickPlanetListener(
				gameManager.universe.planets.get("Naranja")));
		allPlanets = new ButtonGroup();
		planetSelectPanel.add(RojoButton, pspConstraints);
		pspConstraints.gridy++;
		allPlanets.add(RojoButton);
		pspConstraints.gridy++;
		planetSelectPanel.add(AzulButton, pspConstraints);
		allPlanets.add(AzulButton);
		pspConstraints.gridy++;
		planetSelectPanel.add(AmarilloButton, pspConstraints);
		allPlanets.add(AmarilloButton);
		pspConstraints.gridy++;
		planetSelectPanel.add(VerdeButton, pspConstraints);
		allPlanets.add(VerdeButton);
		pspConstraints.gridy++;
		planetSelectPanel.add(NaranjaButton, pspConstraints);
		allPlanets.add(NaranjaButton);

		actionsPanel.add(planetSelectPanel);

		JPanel tradeAndFuelPanel = new JPanel(new GridBagLayout());
		GridBagConstraints tfConstraints = new GridBagConstraints();

		tfConstraints.gridx = 0;
		tfConstraints.gridy = 0;

		tradeButton = new JButton("Trade");
		tradeAndFuelPanel.add(tradeButton, tfConstraints);
		tradeButton.addActionListener(new ClickListener());

		tfConstraints.gridy++;

		JLabel breakLabel = new JLabel("<html><br/></html>");

		tradeAndFuelPanel.add(breakLabel, tfConstraints);

		tfConstraints.gridy++;

		this.fuelUp = new JButton("Fuel Up!");
		tradeAndFuelPanel.add(fuelUp, tfConstraints);
		fuelUp.addActionListener(new FuelListener());

		actionsPanel.add(tradeAndFuelPanel);

		this.add(actionsPanel, mpConstraints);

		setCurrentPlanetVisually();
	}

	public class ClickListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();

			if (source == tradeButton) {

				gameManager.switchScreens(GameSpecifics.TRADE_SCREEN);
			}
			if (source == flyButton && pickPlanet == true) {
				gameManager.switchScreens(GameSpecifics.FLY_SCREEN);
			}

			try {
				gameManager.saveState();
			} catch (ParserConfigurationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (TransformerException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}

	public void setCurrentPlanetVisually() {

		allPlanets.clearSelection();

		switch (gameManager.universe.curPlanet.name) {

		case "Rojo":
			allPlanets.setSelected(RojoButton.getModel(), true);
			break;
		case "Verde":
			allPlanets.setSelected(VerdeButton.getModel(), true);
			break;
		case "Amarillo":
			allPlanets.setSelected(AmarilloButton.getModel(), true);
			break;
		case "Azul":
			allPlanets.setSelected(AzulButton.getModel(), true);
			break;
		case "Naranja":
			allPlanets.setSelected(NaranjaButton.getModel(), true);
			break;

		}

	}

	public class FuelListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {

			if (gameManager.universe.curPlanet.gasStation.fuelUp(
					gameManager.player, gameManager.ship,
					GameSpecifics.GAS_STATION_FILLUP)) {
				fuelLabel
						.setText("Current Fuel: " + gameManager.ship.getTank());
			} else {

				JOptionPane.showMessageDialog(null,
						"Your ship can't hold any more fuel", "ERROR",
						JOptionPane.ERROR_MESSAGE);

			}

			try {
				gameManager.saveState();
			} catch (ParserConfigurationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (TransformerException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}

	}

	public class PickPlanetListener implements ActionListener {

		public Planet travelTo;

		public PickPlanetListener(Planet travelTo) {

			this.travelTo = travelTo;

		}

		@Override
		public void actionPerformed(ActionEvent e) {

			// Before traveling to a planet, we want to:
			/*
			 * If we have enough fuel. When do we have enough fuel? When the
			 * amount of fuel we have is greater than the distance between the
			 * planets
			 */
			if (gameManager.ship.travel(gameManager.universe.curPlanet,
					travelTo)) {

				/* Random events happening... */
				Random generator = new Random();
				int num1;

				num1 = generator.nextInt(100) + 1;
				System.out.println("Random Number: " + num1);
				if (num1 % 3 == 0) {

					gameManager.ship.alienAttack();
					JOptionPane
							.showMessageDialog(
									null,
									"You have been attacked by an Alien while traveling. Now you lose 5 liters of fuel",
									"ERROR", JOptionPane.ERROR_MESSAGE);
				} else if (num1 % 2 == 0) {
					gameManager.ship.freeFuel();
					JOptionPane
							.showMessageDialog(null,
									"It's your lucky day! You get 10 free liters of fuel!");
				}
				locationLabel.setText("<html><h2>Welcome to " + travelTo.name
						+ "</h2></html>");
				fuelLabel
						.setText("Current Fuel: " + gameManager.ship.getTank());
				gameManager.universe.curPlanet = travelTo;

			} else {

				JOptionPane
						.showMessageDialog(null,
								"You do not have enough fuel to go to "
										+ travelTo.name, "ERROR",
								JOptionPane.ERROR_MESSAGE);
				setCurrentPlanetVisually();

			}

			try {
				gameManager.saveState();
			} catch (ParserConfigurationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (TransformerException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}

	}
}
